253C - Text Editor - CodeForces Solution


data structures dfs and similar graphs greedy shortest paths *1600

Please click on ads to support us..

Python Code:

import os
import math
import os
import sys
from bisect import bisect_left, bisect_right, insort
from collections import defaultdict, Counter
from io import BytesIO, IOBase
from types import GeneratorType

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda: self.buffer.read().decode('ascii')
        self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')


def isPrime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i = i + 6
    return True


def lcm(a, b): return (a * b) // math.gcd(a, b)


def ciel(a, b):
    x = a // b
    if a % b == 0:
        return x
    return x + 1


def ints_get(): return map(int, input().strip().split())


def list_get(): return list(map(int, sys.stdin.readline().strip().split()))


def chars_get(): return list(map(str, sys.stdin.readline().strip().split()))


def ipn(): return int(input())


def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc




def main():
        file = open("input.txt", "r")
    ar = file.readline().split()
    n = int(ar[0])
    a = []
    ar = file.readline().split()
    for i in ar:
        a.append(int(i))
    ar = file.readline().split()
    r1, c1, r2, c2 = int(ar[0]), int(ar[1]), int(ar[2]), int(ar[3])
    r1 -= 1
    c1 -= 1
    r2 -= 1
    c2 -= 1
    val = sys.maxsize
    for i in range(0, n):
        x = c1
        if i < r1:
            for j in range(r1 - 1, i - 1, -1):
                x = min(x, a[j])

        else:
            for j in range(r1, i + 1):
                x = min(x, a[j])
        q = abs(r1 - i) + abs(r2 - i)
        if i < r2:
            for j in range(i, r2 + 1):
                x = min(x, a[j])
        else:
            for j in range(i - 1, r2 - 1, -1):
                x = min(x, a[j])
        val = min(val, q + abs(x - c2))

    file.close()
    file1 = open("output.txt", "w")
    L = str(val)
        file1.writelines(L)

    return


if __name__ == "__main__":
    main()


Comments

Submit
0 Comments
More Questions

236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD